package service.impl;

import mapper.AddressMapper;
import mapper.RestaurantMapper;
import mapper.SubscribeMapper;
import mapper.UserMapper;
import org.springframework.stereotype.Service;
import pojo.Address;
import pojo.Restaurant;
import pojo.Subscribe;
import pojo.User;
import service.IUserService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private SubscribeMapper subscribeMapper;

    @Resource
    private RestaurantMapper restaurantMapper;

    @Override
    public int deleteAddress(Integer addId) {
        return addressMapper.deleteByPrimaryKey(addId);
    }

    @Override
    public int setDefaultAddress(Integer addId) {
        // 查询默认地址
        Address address = addressMapper.selectByDefault();
        if (address == null) {
            // 如果没有默认地址,则直接设置当前操作地址为默认地址
            int result = addressMapper.updateDefaultById(addId);
            return result;
        } else {
            int result1 = addressMapper.updateDefaultById(address.getId());
            if (addId != address.getId()) {
                // 如果有默认地址,则修改默认地址
                int result2 = addressMapper.updateDefaultById(addId);
                if (result1 > 0) {
                    if (result2 > 0) {
                        return result2;
                    }
                }
            } else {
                if (result1 > 0) {
                    return result1;
                }
                // 如果设置失败则修改回原来的样子
                addressMapper.updateDefaultById(address.getId());
            }
        }
        return 0;
    }

    @Override
    public int newAddress(Address address) {
        return addressMapper.insert(address);
    }

    @Override
    public int updateAddress(Address address) {
        return addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public List<Address> getAddressList(Integer uid) {
        return this.addressMapper.selectByUserId(uid);
    }

    @Override
    public int recharge(Integer id, BigDecimal value) {
        User user = userMapper.selectByPrimaryKey(id);
        BigDecimal wallet = user.getWallet();
        user.setWallet(wallet.add(value));
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int reset(User user) {
        return this.userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int subscribeRest(Subscribe subscribe) {

        return this.subscribeMapper.insertSelective(subscribe);
    }

    @Override
    public int cancelSubscribe(Subscribe subscribe) {
        Subscribe subscribeRecord = subscribeMapper.selectByUserIdAndRestId(subscribe);
        return this.subscribeMapper.deleteByPrimaryKey(subscribeRecord.getId());
    }

    @Override
    public List<Restaurant> getMyCollections(Map<String, Object> map) {
        // 获取用户收藏列表
        List<Restaurant> list = this.subscribeMapper.selectByUserId(map);
        System.out.println(list);
        return list;
    }

    @Override
    public int selectSubscribeTotal(Map<String, Object> map) {
        return this.subscribeMapper.selectTotal(map);
    }
}
